package com.starlink.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlink.common.PageInfoTwo;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallBooksCategoryDtoMapper;
import com.starlink.model.domain.MallBooksCategoryDO;
import com.starlink.mapper.MallBooksCategoryMapper;
import com.starlink.model.request.MallBooksCategoryRequest;
import com.starlink.model.util.MallBooksCategoryConvert;
import com.starlink.service.MallBooksCategoryService;
import com.starlink.model.vo.MallBooksCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.starlink.utils.IDUtils.getId;

/**
 * @author zhanghong
 */
@Service
public class MallBooksCategoryServiceImpl extends ServiceImpl<MallBooksCategoryMapper, MallBooksCategoryDO> implements MallBooksCategoryService {
    @Autowired
    private MallBooksCategoryMapper mallBooksCategoryMapper;

    @Override
    public MallBooksCategoryVO getByKey(long id) {
        MallBooksCategoryVO mallBooksCategoryVO = MallBooksCategoryDtoMapper.INSTANCE.do2VO(getById(id));
        return mallBooksCategoryVO;
    }

    @Override
    public PageInfoTwo<MallBooksCategoryVO> pageQuery(MallBooksCategoryRequest request) {
        long count = mallBooksCategoryMapper.pageCount(request);
        List<MallBooksCategoryDO> list = mallBooksCategoryMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallBooksCategoryVO> mallBooksCategoryVOList = MallBooksCategoryConvert.buildMallBooksCategoryVoList(list);
        return new PageInfoTwo(request.getPageNum(), request.getPageSize(), mallBooksCategoryVOList, count);
    }

    @Override
    public List<MallBooksCategoryVO> listQuery(MallBooksCategoryRequest request) {
        List<MallBooksCategoryDO> list = mallBooksCategoryMapper.selectDOList(MallBooksCategoryDtoMapper.INSTANCE.request2DO(request));
        List<MallBooksCategoryVO> mallBooksCategoryVOS = MallBooksCategoryConvert.buildMallBooksCategoryVoList(list);
        return mallBooksCategoryVOS;
    }

    @Override
    public Boolean add(MallBooksCategoryRequest request) {
        request.setId(getId());
        save(MallBooksCategoryDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean updateByKey(MallBooksCategoryRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallBooksCategoryDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean deleteDOById(Long id) {
        mallBooksCategoryMapper.deleteDOById(id);
        return true;
    }

    @Override
    public List<MallBooksCategoryVO> selectByBookId(Long mallBooksId) {
        List<MallBooksCategoryDO> mallBooksCategoryDOS = mallBooksCategoryMapper.selectByBookId(mallBooksId);
        List<MallBooksCategoryVO> mallBooksCategoryVOS = MallBooksCategoryConvert.buildMallBooksCategoryVoList(mallBooksCategoryDOS);
        return mallBooksCategoryVOS;
    }
}
